using System;
using System.Linq;
using System.Web.Mvc;
using NHCourse.Domain;
using NHCourse.Models;

namespace NHCourse.Controllers
{
   public class HomeController : AppController
   {
      public ActionResult Index()
      {
         return View();
      }

      public ActionResult Users(int rows, int page, string sidx, string sord)
      {
         IQueryable<User> users = GetDummyUsers();

         if (string.IsNullOrEmpty(sidx))
            sidx = users.GetPropertyName(u => u.DisplayName);

         users = sord == "desc" ? users.OrderByDescending(sidx) : users.OrderBy(sidx);

         var jqGridResult = new JQGridModel<User>(
            rows,
            page,
            users,
            user => user.Id,
            user => new[] {user.DisplayName, user.WebsiteUrl, user.Reputation.ToString()});

         return Json(jqGridResult);
      }

      private IQueryable<User> GetDummyUsers()
      {
         return new[]
            {
               new User {Id = 1, DisplayName = "Jeff Atwood", Reputation = 13460, WebsiteUrl = "http://www.codinghorror.com/blog/"},
               new User {Id = 2, DisplayName = "Geoff Dalgas", Reputation = 761, WebsiteUrl = "http://stackoverflow.com"},
               new User {Id = 3, DisplayName = "Jarrod Dixon", Reputation = 2061, WebsiteUrl = "http://stackoverflow.com"},
               new User {Id = 4, DisplayName = "Joel Spolsky", Reputation = 7769, WebsiteUrl = "http://www.joelonsoftware.com/"},
               new User {Id = 5, DisplayName = "Jon Galloway", Reputation = 13999, WebsiteUrl = "http://weblogs.asp.net/jgalloway/"},
               new User {Id = 8, DisplayName = "Eggs McLaren", Reputation = 168, WebsiteUrl = ""},
               new User {Id = 9, DisplayName = "Kevin Dente", Reputation = 2286, WebsiteUrl = "http://weblogs.asp.net/kdente"},
               new User {Id = 10, DisplayName = "Sneakers O'Toole", Reputation = 101, WebsiteUrl = "http://www.youtube.com/watch?v=qrhNoljPtok"},
               new User {Id = 11, DisplayName = "Anonymous User", Reputation = 167, WebsiteUrl = ""},
               new User {Id = 13, DisplayName = "Chris Jester-Young", Reputation = 31209, WebsiteUrl = "http://careers.stackoverflow.com/cky"},
               new User {Id = 15, DisplayName = "Teifion", Reputation = 5884, WebsiteUrl = "http://woarl.com"},
               new User {Id = 16, DisplayName = "Rodrigo Sieiro", Reputation = 133, WebsiteUrl = "http://twitter.com/rsieiro"},
               new User {Id = 17, DisplayName = "Nick Berardi", Reputation = 13603, WebsiteUrl = "http://www.coderjournal.com"},
               new User {Id = 19, DisplayName = "Mads Kristiansen", Reputation = 86, WebsiteUrl = ""},
               new User {Id = 20, DisplayName = "Tom", Reputation = 4085, WebsiteUrl = ""},
               new User {Id = 22, DisplayName = "maclema", Reputation = 1407, WebsiteUrl = "http://www.maclema.com"},
               new User {Id = 23, DisplayName = "Jax", Reputation = 376, WebsiteUrl = "http://jackstrife.com"},
               new User {Id = 24, DisplayName = "braklet", Reputation = 103, WebsiteUrl = "http://www.google.com/reader/shared/Braklet"},
               new User {Id = 25, DisplayName = "CodingWithoutComments", Reputation = 2977, WebsiteUrl = "http://www.codingwithoutcomments.com"},
               new User {Id = 26, DisplayName = "Shawn Simon", Reputation = 4868, WebsiteUrl = ""},
               new User {Id = 27, DisplayName = "denny", Reputation = 722, WebsiteUrl = "http://www.dennymorrison.com"},
               new User {Id = 29, DisplayName = "Michael Haren", Reputation = 13902, WebsiteUrl = "http://blog.wassupy.com/search/label/Technology"},
               new User {Id = 30, DisplayName = "Grant", Reputation = 2553, WebsiteUrl = "http://beta.stackoverflow.com/badges/22/great-question"},
               new User {Id = 32, DisplayName = "TimM", Reputation = 110, WebsiteUrl = "http://www.sample.com"},
               new User {Id = 33, DisplayName = "Mark", Reputation = 2921, WebsiteUrl = ""},
               new User {Id = 34, DisplayName = "Karl Seguin", Reputation = 4780, WebsiteUrl = "http://www.openmymind.net/"},
               new User {Id = 35, DisplayName = "Greg Hurlman", Reputation = 6107, WebsiteUrl = "http://blog.hurlman.com"},
               new User {Id = 36, DisplayName = "Pat", Reputation = 2091, WebsiteUrl = "http://patpack.blogspot.com"},
               new User {Id = 37, DisplayName = "Wally Lawless", Reputation = 1429, WebsiteUrl = "http://www.power-coder.net"},
               new User {Id = 38, DisplayName = "blcArmadillo", Reputation = 333, WebsiteUrl = "http://www.ianburris.com"},
               new User {Id = 39, DisplayName = "huseyint", Reputation = 3665, WebsiteUrl = "http://huseyint.com/"},
               new User {Id = 40, DisplayName = "Kevin", Reputation = 2390, WebsiteUrl = "http://kevinx.net/"},
               new User {Id = 41, DisplayName = "Redbaron", Reputation = 212, WebsiteUrl = ""},
               new User {Id = 42, DisplayName = "Coincoin", Reputation = 5868, WebsiteUrl = ""},
               new User {Id = 43, DisplayName = "Justin", Reputation = 378, WebsiteUrl = ""},
               new User {Id = 44, DisplayName = "Tom Grochowicz", Reputation = 212, WebsiteUrl = "http://www.crabwisestudios.com"},
               new User {Id = 45, DisplayName = "Bob", Reputation = 10351, WebsiteUrl = "http://www.bob-stanton.com"},
               new User {Id = 46, DisplayName = "Sven", Reputation = 3164, WebsiteUrl = "http://-"},
               new User {Id = 48, DisplayName = "Mat", Reputation = 919, WebsiteUrl = "http://www.jackbrand.co.uk"},
               new User {Id = 49, DisplayName = "Leon Bambrick", Reputation = 4461, WebsiteUrl = "http://secretGeek.net"},
               new User {Id = 50, DisplayName = "contagious", Reputation = 2529, WebsiteUrl = "http://andrebluehs.net"},
               new User {Id = 51, DisplayName = "Yaakov Ellis", Reputation = 5093, WebsiteUrl = "http://ellisweb.net"},
               new User {Id = 52, DisplayName = "saniul", Reputation = 1926, WebsiteUrl = ""},
               new User {Id = 53, DisplayName = "redwards", Reputation = 21, WebsiteUrl = ""},
               new User {Id = 55, DisplayName = "Ryan Fox", Reputation = 2756, WebsiteUrl = "http://rcfox.ca"},
               new User {Id = 56, DisplayName = "doekman", Reputation = 1415, WebsiteUrl = "http://zanstra.com/base/blog"},
               new User {Id = 57, DisplayName = "Chad Paulson", Reputation = 13, WebsiteUrl = "http://chadpaulson.com/about.html"},
               new User {Id = 58, DisplayName = "cmcculloh", Reputation = 2923, WebsiteUrl = "http://blog.chomperstomp.com"},
               new User {Id = 59, DisplayName = "saint_groceon", Reputation = 1191, WebsiteUrl = "http://www.antesonic.org"},
               new User {Id = 60, DisplayName = "Dave Ward", Reputation = 8308, WebsiteUrl = "http://encosia.com"}
            }
            .AsQueryable();
      }
   }
}